﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace LocalOverride.Fiddler.UI
{
    public partial class EditMapping : UserControl
    {
        public event EventHandler<EventArgs> SaveEdit;

        public void OnSaveEdit()
        {
            EventHandler<EventArgs> handler = SaveEdit;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        public event EventHandler<EventArgs> CancelEdit;

        public void OnCancelEdit()
        {
            var handler = CancelEdit;
            if (handler != null) handler(this, EventArgs.Empty);
        }

        public EditMapping()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            OnSaveEdit();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            OnCancelEdit();
        }

        public string Url
        {
            get { return txtUrl.Text; }
            set { txtUrl.Text = value; }
        }

        public string LocalPath
        {
            get { return txtLocalPath.Text; }
            set { txtLocalPath.Text = value; }
        }

        public string DefaultFileName
        {
            get { return txtDefaultFileName.Text; }
            set { txtDefaultFileName.Text = value; }
        }

        public bool IsDirectory
        {
            get { return rbtnDirectory.Checked; }
            set
            {
                if (value)
                {
                    rbtnDirectory.Checked = true;
                }
                else
                {
                    rbtnFile.Checked = true;
                }
            }

        }

        public string Extensions
        {
            get { return txtExtensions.Text; }
            set { txtExtensions.Text = value; }
        }

        public bool DownloadLocalCopy
        {
            get { return chkDownloadLocalCopy.Checked; }
            set { chkDownloadLocalCopy.Checked = value; }
        }

        public void SetErrors(IEnumerable<ErrorNotification> notifications)
        {
            ClearErrors();
            foreach (var errorNotification in notifications)
            {
                var control = GetControlFromFieldName(errorNotification.Field);

                if (control != null)
                    errorProvider.SetError(control, errorNotification.Message);

            }
        }

        private Control GetControlFromFieldName(string field)
        {
            switch (field)
            {
                case "Url":
                    return txtUrl;
                case "LocalPath":
                    return txtLocalPath;
                case "Extensions":
                    return txtExtensions;
                default:
                    return null;
            }
        }

        public void ClearErrors()
        {
            errorProvider.Clear();
        }
    }
}
